package com.apex.crm.controller;

import com.apex.crm.base.BaseController;
import com.apex.crm.base.ResultInfo;
import com.apex.crm.exceptions.ParamsException;
import com.apex.crm.model.UserModel;
import com.apex.crm.query.UserQuery;
import com.apex.crm.service.UserService;
import com.apex.crm.utils.LoginUserUtil;
import com.apex.crm.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author XiaoYang
 * @version 1.0
 * @program: crm
 * @Classname UserController
 * @description TODO
 * @date 2021/4/21 8:49
 */

@Controller
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @PostMapping("user/login")
    @ResponseBody
    public ResultInfo userLogin(String userName,String userPassword){
        ResultInfo resultInfo = new ResultInfo();
        UserModel userModel = userService.userLogin(userName,userPassword);
        resultInfo.setResult(userModel);

        return resultInfo;
    }

    //try catch
//    @PostMapping("user/login")
//    @ResponseBody
//    public ResultInfo userLogin(String userName,String userPassword){
//        ResultInfo resultInfo = new ResultInfo();
//        try{
//            UserModel userModel = userService.userLogin(userName,userPassword);
//            resultInfo.setResult(userModel);
//        }catch (Exception e){
//            e.printStackTrace();
//            resultInfo.setCode(500);
//            resultInfo.setMsg("操作失败！");
//        }
//        return resultInfo;
//    }


    //用户密码修改
    @PostMapping("user/updatePassword")
    @ResponseBody
    public ResultInfo updateUserPassword(HttpServletRequest request,String oldPassword,String newPassword,String confirmPassword){
        ResultInfo resultInfo = new ResultInfo();
        Integer userId= LoginUserUtil.releaseUserIdFromCookie(request);
        userService.updateUserPassword(userId,oldPassword,newPassword,confirmPassword);

        return resultInfo;
    }

//    //用户密码修改  try catch
//    @PostMapping("user/updatePassword")
//    @ResponseBody
//    public ResultInfo updateUserPassword(HttpServletRequest request,String oldPassword,String newPassword,String confirmPassword){
//        ResultInfo resultInfo = new ResultInfo();
//        try{
//            Integer userId= LoginUserUtil.releaseUserIdFromCookie(request);
//            userService.updateUserPassword(userId,oldPassword,newPassword,confirmPassword);
//        }catch (ParamsException e){
//            e.printStackTrace();
//            resultInfo.setCode(e.getCode());
//            resultInfo.setMsg(e.getMsg());
//        }catch (Exception e){
//            e.printStackTrace();
//            resultInfo.setCode(500);
//            resultInfo.setMsg("操作失败！");
//        }
//        return resultInfo;
//    }

    //跳转到修改密码界面
    @RequestMapping("user/toPasswordPage")
    public String toPasswordPage(){
        return "user/password";
    }



    /**
     * 查询所有的销售人员
     * @return
     */
    @RequestMapping("user/queryAllSales")
    @ResponseBody
    public List<Map<String, Object>> queryAllSales() {
        //System.out.println("查询全部");
        System.out.println(userService.queryAllSales());
        return userService.queryAllSales();
    }

    @GetMapping("user/list")
    @ResponseBody
    public Map<String,Object> queryUserByParams(UserQuery userQuery){
        //System.out.println("查询部分");
        return userService.queryUserByParams(userQuery);
    }

    @GetMapping("user/index")
    public String index(){
        return "user/user";
    }



    @PostMapping("user/save")
    @ResponseBody
    public ResultInfo saveUser(User user){
        userService.saveUser(user);
        return success("用户记录添加成功");
    }

    @PostMapping("user/update")
    @ResponseBody
    public ResultInfo updateUser(User user){
        userService.updateUser(user);
        return success("用户记录更新成功");
    }

    @PostMapping("user/delete")
    @ResponseBody
    public ResultInfo deleteUser(Integer[] ids){
        userService.deleteUserByIds(ids);
        return success("用户记录删除成功");
    }

    @GetMapping("user/addOrUpdateUserPage")
    public String addOrUpdateUserPage(Integer id, Model model){
        model.addAttribute("userUpdate",userService.getById(id));
        return "user/add_update";
    }


    /**
     * @author XiaoYang
     * @description  查询客户经理
     * @date  19:15 2021/5/19
     * @param
     * @exception
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @version 1.0
     */
    @PostMapping("user/queryAllCustomerManager")
    @ResponseBody
    public List<Map<String,Object>> queryAllCustomerManager(){
        return userService.queryAllCustomerManager();
    }




}
